<template>
  <div class="pic-bed">
    <div class="upload-board">
      <div class="preview">
        <img v-if="filename" :src="url">
        <span v-else>图片预览区</span>
      </div>
      <!-- 上传图片 -->
      <div class="upload-btn">
        <el-button type="primary" @click="handleUpload">上传图片</el-button>
        <input
          id="file"
          ref="file"
          style="display: none"
          type="file"
          accept="image/*"
          @click="e => { e.target.value = ''; }"
          @change="uploadFile">
      </div>
    </div>
    <div class="input-form">
      <el-form inline class="copy-items" label-width="140px">
        <el-form-item label="url">
          <el-input v-model="url" autosize placeholder="请上传图片" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="copy-url" @click="copyItem('url')">复制</el-button>
        </el-form-item>
      </el-form>
      <el-form inline class="copy-items" label-width="140px">
        <el-form-item label="markdown">
          <el-input v-model="markdown" placeholder="请上传图片" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="copy-md" @click="copyItem('md')">复制</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import Clipboard from 'clipboard'
const FileBaseUrl = process.env.VUE_APP_FILE_BASE_URL
export default {
  components: {},
  props: {},
  data() {
    return {
      url: '',
      filename: '',
      token: '',
      key: '', // 静态密钥
      dialogVisible: true // 校验弹窗
    }
  },
  computed: {
    markdown() {
      if (this.filename && this.url) {
        return `![${this.filename}](${this.url})`
      } else {
        return ''
      }
    }
  },
  methods: {
    handleUpload() {
      this.$refs.file.dispatchEvent(new MouseEvent('click'))
    },
    // 上传图片到后台
    async uploadFile() {
      const file = this.$refs.file.files[0]
      const formData = new FormData()
      formData.append('file', file)
      this.$http.common.upload(formData, '/api/File/PicBedUpload').then(res => {
        const fileInfo = res.data.data
        const { filename, path } = fileInfo
        this.filename = filename
        this.url = FileBaseUrl + path
      })
    },
    // 复制文字
    copyItem(selector) {
      switch (selector) {
        case 'url':
          this.copyMethod('.copy-url', this.url)
          break
        case 'md':
          this.copyMethod('.copy-md', this.markdown)
          break
      }
    },
    copyMethod(selector, item) {
      const clipboard = new Clipboard(selector, {
        text: () => {
          return item
        }
      })
      clipboard.on('success', () => {
        this.$notify({
          type: 'success',
          title: '成功',
          message: '复制成功！'
        })
        clipboard.destroy()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep {
  .el-input__inner {
    width: 800px;
  }
}
.pic-bed {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  background-color: #fff;

  .upload-board {
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .preview {
    width: 800px;
    height: 500px;
    border: 2px dotted rgb(168, 168, 168);
    border-radius: 10px;
    overflow: hidden;
    text-align: center;
    line-height: 500px;
    color: rgb(168, 168, 168);
    background-color: #fff;

    img {
      width: 800px;
      height: 500px;
    }
  }
  .input-form {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
  }
  .upload-btn, .copy-url, .copy-md {
    margin-left: 20px;
  }
  .copy-items {
    position: relative;
    left: -75px;

    &:first-child {
      margin-top: 20px;
    }
  }
}
</style>
